/*
 * $Id: autostr.h,v 1.18 2001/01/30 21:32:41 dms Exp $
 *
 *
 */


#ifndef autostr_h
#define autostr_h

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif


#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef HAVE_STL_STRING
#include <typeinfo>
#endif

#include "MyException.h"

inline int equ(char *a, char *b)
{
    if (a == 0 && b == 0 ) return 1;
    if (a == 0 || b == 0 ) return (a-b);
    return (strcmp(a,b) == 0);
}

inline int equ(char *a, char *b, int n)
{
    if (a == 0 && b == 0 ) return 1;
    if (a == 0 || b == 0 ) return (a-b);
    return (strncmp(a,b,n) == 0);
}


char *strnchr(char *s, char c, int len);
char *strnpbrk(char *s, char * chars, int len);

/* split string into dinamicaly allocated strings
 * all arguments must be char **, last is 0
 */
#ifdef HAVE_STL_STRING
int ssplit(char delem, const string& str, ... );
int ssplit(char delem, const string& str, const int nf, ... );

int sunsplit(char delem, const string *str, ... );

/* xsplit - split with autodetect */


class splitable_base
{
public:
   string tn;
};

template <class T>
class splitable : public splitable_base
{
public:
   T d;
   splitable(){ tn = typeid(T).name(); };
};

int xsplit(char delem, const string& str, ... );
// int xsplit(char delem, const string& str, const int nf, ... );

#endif

int split(char delem, char *str, ... );
int split(char delem, char *str, int nf, ... );

char *unsplit(char delem, ... );

int wsplit(char *str, int len, int nf, ... );
int wssplit(char *str, int len, int nf, ... );


/* sample string manipulation
   I don't try overgo GNU Strings but ...
 */

DECLARE_EXCEPTION(AutoStr);

class autostr
{
public:
  char *s;
  int sz;

  autostr(){ s = 0; }
  autostr(char *src);
  autostr(const char *src);
  autostr(char *src, int size);
  autostr(int size);
 ~autostr(){ if(s) delete s; }
  char * sprintf(char *format, ... );
  char& operator[](int i);
  autostr& operator +(char *ns);

  autostr& operator +(autostr& as);

  autostr& operator =(char * src);

  autostr& dup(char *src){ s = src; sz = strlen(src); return *this; }

 int size(){ return sz; }
 void ncpy(char *src, int n);

 char *c_str(){ return s; }
 operator char *(){ return c_str(); }

 operator int (){ return strlen(s); }
 int operator == (char *ns){ return (strcmp(s,ns) == 0); }
 int operator != (char *ns){ return (strcmp(s,ns) != 0); }

};

#endif
